package com.pan.note.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pan.note.dao.ChatInfoDao;
import com.pan.note.dao.UserDao;
import com.pan.note.mapper.ChatInfoMapper;
import com.pan.note.mapper.UserMapper;
import com.pan.note.service.UserService;
import com.pan.note.vo.UserDisplayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户相关服务实现类
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ChatInfoMapper chatInfoMapper;

    /**
     * 根据用户id查询该用户的好友
     * @return 查询到的结果集
     * @param accountId 传入的用户id
     */
    @Override
    public List<UserDisplayVo> dispalyUserList(String accountId) {
        List<UserDisplayVo> userDisplayVos= new ArrayList<>();
        //TODO
//        暂时查询所有用户，好友系统稍等一下
        //mybatis-plus 查询条件
//        QueryWrapper<UserDto> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("account_id",accountId);
        List<UserDao> userDtos = userMapper.selectList(null);
        //赋值
        userDtos.forEach(dto -> {
            UserDisplayVo userDisplayVo = new UserDisplayVo();
            BeanUtils.copyProperties(dto,userDisplayVo);
            userDisplayVos.add(userDisplayVo);
        });
        log.info("查询好友列表成功");
        return userDisplayVos;
    }

    /**
     * 根据用户和好友id查询未读消息数量
     * @param accountId 用户id
     * @param friendList 好友列表
     * @return 未读消息数量
     */
    @Override
    public Map<String, Integer> findNotCheckMsg(String accountId, List<UserDisplayVo> friendList) {
        Map<String,Integer> result = new HashMap<>();
        friendList.forEach(dto -> {
            QueryWrapper<ChatInfoDao> queryWrapper1 = new QueryWrapper<>();
            QueryWrapper<ChatInfoDao> queryWrapper2 = new QueryWrapper<>();
            // 来自我发给对方的信息
            queryWrapper1.eq("sender",accountId);
            queryWrapper1.eq("receiver",dto.getAccountId());
            queryWrapper1.eq("chat_check","0");
            List<ChatInfoDao> chatInfosFromMe = chatInfoMapper.selectList(queryWrapper1);
            // 来自对方发给我的信息
            queryWrapper2.eq("sender",dto.getAccountId());
            queryWrapper2.eq("receiver",accountId);
            queryWrapper2.eq("chat_check","0");
            List<ChatInfoDao> chatInfoFromHe = chatInfoMapper.selectList(queryWrapper2);
            //统计数量
            Integer count = chatInfosFromMe.size() + chatInfoFromHe.size();
            result.put(dto.getAccountId(),count);
        });
        log.info("查询未读消息数量成功");
        return result;
    }

    /**
     * 查找我自己
     * @param accountId 我自己的id
     * @return 我自己的信息
     */
    @Override
    public UserDisplayVo findMe(String accountId) {
        UserDisplayVo userDisplayVo = new UserDisplayVo();
        QueryWrapper<UserDao> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",accountId);
        List<UserDao> userDaos = userMapper.selectList(queryWrapper);
        BeanUtils.copyProperties(userDaos.get(0),userDisplayVo);
        return userDisplayVo;
    }
}
